using System;
using System.Collections.Generic;
using System.Text;

namespace BAFIRCSharp
{
    public struct SenderInf
    {
        public string Nickname;
        public string Username;
        public string Hostname;

        public override string ToString()
        {
            return Nickname + "!" + Username + "@" + Hostname;
        }
    }

    public class IRCMessage
    {
        public string Message; // Full message
        public bool IsNumeric;
        public bool HasSenderInf;
        public SenderInf Sender; // Sender Information
        public string Command; // IRC Command
        public Int32 CommandNumeric;
        public List<string> Params;

        public IRCMessage()
        {
            Sender = new SenderInf();
            Params = new List<string>();
        }

        public IRCMessage(string Msg)
        {
            Sender = new SenderInf();
            Params = new List<string>();

            Parse(Msg);
        }

        public void Parse(string Msg)
        {
            Msg = Msg.Trim();
            Message = Msg;
            string Rest;

            Sender.Nickname = "";
            Sender.Username = "";
            Sender.Hostname = "";

            // <message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
            if (Msg[0] == ':') // There is a prefix, so parse it.
            {
                HasSenderInf = true;
                string Prefix = Msg.Substring(1, Msg.IndexOf(' ')).Trim();
                Rest = Msg.Substring(Msg.IndexOf(' ') + 1).Trim();
                if (Prefix.IndexOf('!') != -1)
                {
                    Sender.Nickname = Prefix.Substring(0, Prefix.IndexOf('!'));
                    if (Prefix.IndexOf('@') != -1)
                    {
                        Sender.Username = Prefix.Substring(Prefix.IndexOf('!') + 1, Prefix.IndexOf('@') - (Prefix.IndexOf('!') + 1)).Trim();
                        Sender.Hostname = Prefix.Substring(Prefix.IndexOf('@') + 1).Trim();
                    }
                    else
                        Sender.Username = Prefix.Substring(Prefix.IndexOf('!') + 1).Trim();
                }
                else
                    Sender.Nickname = Prefix.Trim();
            }
            else // No Prefix
            {
                HasSenderInf = false;
                Rest = Msg;
            }

            Command = Rest.Substring(0, Rest.IndexOf(' ')).Trim().ToUpper();
            string LastParam = "";
            if(Rest.IndexOf(" :") != -1)
                LastParam = Rest.Substring(Rest.IndexOf(" :") + 2).Replace('\r', ' ').Replace('\n', ' ').Trim();
            char[] ParamSplit = { ' ' };
            int ParmsTill = (Rest.IndexOf(" :") + 1) - (Rest.IndexOf(' ') + 1);
            if(ParmsTill < 0)
                ParmsTill = Rest.Length - (Rest.IndexOf(' ') + 1);
            string[] Parms = Rest.Substring(Rest.IndexOf(' ') + 1, ParmsTill).Trim().Split(ParamSplit);
            foreach (string ThisParam in Parms)
                if(ThisParam.Trim() != string.Empty)
                    Params.Add(ThisParam);
            if (LastParam != "")
                Params.Add(LastParam);

            if (Int32.TryParse(Command, out CommandNumeric))
                IsNumeric = true;
        }
    }
}
